package ms.shop.service;

import com.github.pagehelper.PageHelper;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.bean.BeanUtil;
import ms.common.util.string.StringUtil;
import ms.shop.bean.GoodParamBean;
import ms.shop.bean.GoodQueryParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.dao.ClassifyDAO;
import ms.shop.dao.GoodDAO;
import ms.shop.entity.Classify;
import ms.shop.entity.Good;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Project Name: ms
 * File Name: GoodService
 * Package Name: ms.shop.service
 * Date: 2018/5/31  16:28
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class GoodService {
    
    @Autowired
    private GoodDAO goodDAO;

    @Autowired
    private ClassifyDAO classifyDAO;
    /**
     * 分页查询
     * @param goodQueryParamBean
     * @param key
     * @return
     */
    @Cacheable(value = "queryGoodPage", key = "'queryGoodPage' + #goodQueryParamBean.pageNo + '-' + #goodQueryParamBean.getStartPrice() + '-' + #goodQueryParamBean.getEndPrice() + '-' + #goodQueryParamBean.getStatus()+ '-' + #goodQueryParamBean.getClassifyId()", condition="#key==''" )
    public Page<Good> query(GoodQueryParamBean goodQueryParamBean, String key) {
        //使用pageHelper插件进行分页查询
        com.github.pagehelper.Page page = PageHelper.startPage(goodQueryParamBean.getPageNo(), goodQueryParamBean.getPageSize());
        List<Good> goods = goodDAO.queryGoods(goodQueryParamBean.getName(), goodQueryParamBean.getStartPrice(), goodQueryParamBean.getEndPrice(),goodQueryParamBean.getStatus());

        return new Page(page.getPageNum(), page.getPageSize(), (int)page.getTotal(), goods);
    }

    /**
     * 新增
     * @param goodParamBean
     */
    @CacheEvict(value= {"queryGoodPage"},allEntries=true)// 清空 user_detail queryPage 缓存
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void create(GoodParamBean goodParamBean) {
        //判断分类信息
        Classify classify = classifyDAO.selectByPrimaryKey(goodParamBean.getClassifyId());
        if(null == classify) {
            throw new BizException(ResponseCode.GOOD_UN_EXIST, "分类不存在");
        }
        //新增
        Good good = BeanUtil.copy(goodParamBean, Good.class);
        good.setStatus(goodParamBean.getStatus());
        good.setCreateTime(System.currentTimeMillis());
        good.setUpdateTime(System.currentTimeMillis());

        goodDAO.insert(good);
    }

    /**
     * 修改
     * @param goodParamBean
     */
    @Caching(put = @CachePut(value = "good_detail", key = "'good_detail' + #goodParamBean.id"), evict = { @CacheEvict(value = "queryGoodPage", allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void update(GoodParamBean goodParamBean) {
        Good currentGood = goodDAO.selectByPrimaryKey(goodParamBean.getId());
        if(StringUtil.isEmpty(currentGood)) {
            throw new BizException(ResponseCode.GOOD_UN_EXIST, "商品不存在");
        }
        //判断分类信息
        Classify classify = classifyDAO.selectByPrimaryKey(goodParamBean.getClassifyId());
        if(null == classify) {
            throw new BizException(ResponseCode.GOOD_UN_EXIST, "分类不存在");
        }
        //修改
        Good good = BeanUtil.copy(goodParamBean, Good.class);
        if(StringUtil.isEmpty(good.getImg())) {
            good.setImg(currentGood.getImg());
        }
        good.setStatus(null);
        good.setCreateTime(null);
        good.setUpdateTime(System.currentTimeMillis());

        goodDAO.updateByPrimaryKeySelective(good);
    }

    /**
     * 启用
     * @param id
     */
    @Caching(put = @CachePut(value = "good_detail", key = "'good_detail' + #id"), evict = { @CacheEvict(value = "queryGoodPage", allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void use(Long id) {
        Good good = goodDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(good)) {
            throw new BizException(ResponseCode.GOOD_UN_EXIST, "商品不存在");
        }
        if(1 == good.getStatus()) {
            throw new BizException(ResponseCode.GOOD_STATUS_ERROR, "商品状态错误");
        }
        good.setStatus(1);
        goodDAO.updateByPrimaryKeySelective(good);
    }

    /**
     * 停用
     * @param id
     */
    @Caching(put = @CachePut(value = "good_detail", key = "'good_detail' + #id"), evict = { @CacheEvict(value = "queryGoodPage", allEntries = true)})
    @Transactional(rollbackFor = Exception.class, isolation = Isolation.READ_COMMITTED)
    public void cancel(Long id) {
        Good good = goodDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(good)) {
            throw new BizException(ResponseCode.GOOD_UN_EXIST, "商品不存在");
        }
        if(2 == good.getStatus()) {
            throw new BizException(ResponseCode.GOOD_STATUS_ERROR, "商品状态错误");
        }
        good.setStatus(2);
        goodDAO.updateByPrimaryKeySelective(good);
    }

    /**
     * 详情
     * @param id
     */
    @Cacheable(value = "good_detail", key = "'good_detail' + #id")
    public Good detail(Long id) {
        Good good = goodDAO.selectByPrimaryKey(id);
        if(StringUtil.isEmpty(good)) {
            throw new BizException(ResponseCode.GOOD_UN_EXIST, "商品不存在");
        }
        return good;
    }

    @CacheEvict(value = {"queryGoodPage", "good_detail"}, allEntries = true)
    public void clear() {
    }
}
